package algoWing42.vista;

import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.GridBagLayout;
import java.awt.Rectangle;
import javax.swing.JButton;

import algoWing42.controlador.AccionBotonGuardar;

import titiritero.vista.KeyPressedController;
import titiritero.vista.Panel;
import titiritero.ControladorJuego;
import titiritero.SuperficieDeDibujo;
import java.awt.ComponentOrientation;

public class VentanaJuego extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JPanel jPanel = null;
	private JPanel panelSuperior = null;
	private Panel panelCentral = null;
	private JButton botonContinuar = null;
	private JButton botonPausar = null;
	private JButton botonSalir = null;
	private ControladorJuego controlador;
	private JButton botonGuardar = null;

	/**
	 * This is the default constructor
	 */
	public VentanaJuego() {
		super();
		initialize();
	}

	public VentanaJuego(ControladorJuego controlador) {

		super();
		this.controlador = controlador;
		this.addKeyListener(new KeyPressedController(controlador));
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(900, 740);
		this.setContentPane(getJContentPane());
		this.setTitle("JFrame");
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getJPanel(), null);
			jContentPane.add(getPanelSuperior(), null);
			jContentPane.add(getPanelCentral(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			jPanel = new JPanel();
			jPanel.setLayout(new GridBagLayout());
			jPanel.setBounds(new Rectangle(0, 0, 784, 0));
		}
		return jPanel;
	}

	/**
	 * This method initializes panelSuperior
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getPanelSuperior() {
		if (panelSuperior == null) {
			panelSuperior = new JPanel();
			panelSuperior.setLayout(null);
			panelSuperior.setBounds(new Rectangle(0, 0, 685, 63));
			panelSuperior.add(getBotonUno(), null);
			panelSuperior.add(getJButton(), null);
			panelSuperior.add(getBotonTres(), null);
			panelSuperior.add(getBotonGuardar(), null);
		}
		return panelSuperior;
	}

	/**
	 * This method initializes panelCentral
	 * 
	 * @return javax.swing.JPanel
	 */
	private Panel getPanelCentral() {
		if (panelCentral == null) {
			panelCentral = new Panel(900, 677, this.controlador);
			panelCentral.setLayout(new GridBagLayout());
			panelCentral.setBounds(new Rectangle(0, 63, 900, 677));
			panelCentral
					.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
		}
		return panelCentral;
	}

	/**
	 * This method initializes botonUno
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getBotonUno() {
		if (botonContinuar == null) {
			botonContinuar = new JButton();
			botonContinuar.setBounds(new Rectangle(14, 14, 119, 30));
			botonContinuar.setEnabled(false);
			botonContinuar.setText("Continuar");
			botonContinuar
					.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {

							controlador.comenzarJuegoAsyn();
							botonContinuar.setEnabled(false);
							getJButton().setEnabled(true);
						}
					});
		}
		return botonContinuar;
	}

	/**
	 * This method initializes jButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButton() {
		if (botonPausar == null) {
			botonPausar = new JButton();
			botonPausar.setBounds(new Rectangle(144, 14, 123, 30));
			botonPausar.setText("Pausar");
			botonPausar.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					controlador.detenerJuego();
					getBotonUno().setEnabled(true);
					getJButton().setEnabled(false);

				}
			});
		}
		return botonPausar;
	}

	/**
	 * This method initializes botonTres
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getBotonTres() {
		if (botonSalir == null) {
			botonSalir = new JButton();
			botonSalir.setBounds(new Rectangle(550, 14, 118, 30));
			botonSalir.setText("Salir");
			botonSalir
					.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
						public void propertyChange(
								java.beans.PropertyChangeEvent e) {
							if ((e.getPropertyName().equals("text"))) {
								System.out.println("propertyChange(text)"); // TODO
																			// Auto-generated
																			// property
																			// Event
																			// stub
																			// "text"
							}
						}
					});
			botonSalir.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.exit(0);
				}
			});
		}
		return botonSalir;
	}

	public SuperficieDeDibujo getSuperficieDeDibujo() {
		return this.panelCentral;
	}

	public ControladorJuego getControlador() {
		return this.controlador;
	}

	/**
	 * This method initializes botonGuardar
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getBotonGuardar() {
		if (botonGuardar == null) {
			botonGuardar = new JButton();
			botonGuardar.setBounds(new Rectangle(274, 16, 117, 28));
			botonGuardar.setText("Guardar");
			botonGuardar.addActionListener(new AccionBotonGuardar(this));

		}
		return botonGuardar;
	}

}
